﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class SlgEntityBullet : BattleEntity
{
    public GameObject m_pGameObj;
    public FlyItem m_pMoveComp;
    public Action onArrive;

    public override void OnCreate(params object[] args)
    {
        m_pGameObj = new GameObject();
        m_pGameObj.name = "Entity_SlgBullet_" + Id.ToString();
        if (m_pMoveComp == null)
            m_pMoveComp = m_pGameObj.AddComponent<FlyItem>();

        FlyItemStateType flyType = (FlyItemStateType)args[0];
        int renderId = (int)args[1];
        Vector3 bornPos = (Vector3)args[2];
        Vector3 targetPos = (Vector3)args[3];
        float speed = Convert.ToSingle(args[4]);
        float angle = 0;
        bool inSky = true;
        m_pGameObj.transform.position = bornPos;
        BattleEffectObj renderObj = BattleEffectMgr.Instance.CreateEffectObj(renderId, Id, m_pGameObj.transform, Vector3.zero);
        if (flyType == FlyItemStateType.Parabola)
        {
            angle = Convert.ToSingle(args[5]);
            inSky = Convert.ToBoolean(args[6]);
            m_pMoveComp.InitParabola(m_pGameObj.transform.position, targetPos, speed, angle, inSky, OnArrive, false);
        }
        else if(flyType == FlyItemStateType.Line)
        {
            inSky = Convert.ToBoolean(args[5]);
            m_pMoveComp.InitLine(m_pGameObj.transform.position, targetPos, speed,inSky, OnArrive, false);
            
        }
    }

    public override void OnUpdate(float deltaTime)
    {
        base.OnUpdate(deltaTime);
        m_pMoveComp.OnUpdate(deltaTime);
    }

    private void OnArrive(FlyItem item)
    {
        if (ParentNotNull())
        {
            EntityMessageDispatcher.Instance.SendMessage(MessageId.BulletArrive, Id, GetParent().Id);
        }
        if (onArrive != null)
        {
            onArrive();
        }
        BattleEntityFactory.Instance.DestroyEntity(Id);
        BattleEffectMgr.Instance.DelEntityAllEffObj(Id);
    }

    public void RegOnArrive(Action func)
    {
        onArrive = func;
    }

    public override void OnDestroy()
    {
        GameObject.DestroyObject(m_pGameObj);
    }
}
